//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbXa_Phuong
    {
        //-----------------------------------------------------------//
        public static bool KiemTraDuLieu(ETbXa_Phuong Xa_Phuong)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenXa_Phuong", Xa_Phuong.TenXa_Phuong);
         
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbXa_Phuong_KiemTraDuLieu", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        //-----------------------------------------------------------//
        public static bool KiemTraDuLieu_Sua(ETbXa_Phuong Xa_Phuong)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenXa_Phuong", Xa_Phuong.TenXa_Phuong);
            pr[1] = new SqlParameter(@"MaXa_Phuong", Xa_Phuong.MaXa_Phuong);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbXa_Phuong_KiemTraDuLieu_Sua", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        //-----------------------------------------------------------//
        public static DataTable SearchXa_Phuong(ETbXa_Phuong Xa_Phuong)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenXa_Phuong", Xa_Phuong.TenXa_Phuong);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbXa_Phuong_Search", pr);
            return all;
        }
        //-------------------------------------------------------------------
        public static DataTable SelectAll1()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbXa_Phuong_SelectAll1", null);
            return all;
        }
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbXa_Phuong_SelectAll", null);
            return all;
        }
    
//-----------------------------------------------------------//
        public static void Insert(ETbXa_Phuong OTbXa_Phuong)
        {
            SqlParameter[] pr = new SqlParameter[4];
            pr[0] = new SqlParameter(@"MaXa_Phuong", OTbXa_Phuong.MaXa_Phuong);
            pr[1] = new SqlParameter(@"TenXa_Phuong", OTbXa_Phuong.TenXa_Phuong);
            pr[2] = new SqlParameter(@"MaHuyen_Quan", OTbXa_Phuong.MaHuyen_Quan);
            pr[3] = new SqlParameter(@"TrangThai", OTbXa_Phuong.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbXa_Phuong_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbXa_Phuong OTbXa_Phuong)
        {
            SqlParameter[] pr = new SqlParameter[4];
            pr[0] = new SqlParameter(@"MaXa_Phuong", OTbXa_Phuong.MaXa_Phuong);
            pr[1] = new SqlParameter(@"TenXa_Phuong", OTbXa_Phuong.TenXa_Phuong);
            pr[2] = new SqlParameter(@"MaHuyen_Quan", OTbXa_Phuong.MaHuyen_Quan);
            pr[3] = new SqlParameter(@"TrangThai", OTbXa_Phuong.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbXa_Phuong_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaXa_Phuong)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaXa_Phuong",MaXa_Phuong);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbXa_Phuong_Delete", pr);
        }
//-----------------------------------------------------------//
        public static ETbXa_Phuong SelectByID(int MaXa_Phuong)
        {
            ETbXa_Phuong OTbXa_Phuong = new ETbXa_Phuong();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaXa_Phuong",MaXa_Phuong);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbXa_Phuong_SelectByID", pr);
            if (idr.Read())
              OTbXa_Phuong = GetOneTbXa_Phuong(idr);
            idr.Close();
            idr.Dispose();
            return OTbXa_Phuong;
        }
//-----------------------------------------------------------//
        private static ETbXa_Phuong GetOneTbXa_Phuong(IDataReader idr)
       {
            ETbXa_Phuong OTbXa_Phuong = new ETbXa_Phuong();
                if (idr["MaXa_Phuong"] != DBNull.Value)
                    OTbXa_Phuong.MaXa_Phuong = (int)idr["MaXa_Phuong"];
                if (idr["TenXa_Phuong"] != DBNull.Value)
                    OTbXa_Phuong.TenXa_Phuong = (string)idr["TenXa_Phuong"];
                if (idr["MaHuyen_Quan"] != DBNull.Value)
                    OTbXa_Phuong.MaHuyen_Quan = (int)idr["MaHuyen_Quan"];
                if (idr["TrangThai"] != DBNull.Value)
                    OTbXa_Phuong.TrangThai = (int)idr["TrangThai"];
            return OTbXa_Phuong;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaXa_Phuong)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaXa_Phuong",MaXa_Phuong);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbXa_Phuong_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbXa_Phuong> ListAll()
        {
            List<ETbXa_Phuong> list = new List<ETbXa_Phuong>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbXa_Phuong_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbXa_Phuong(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}
